﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbbTimetable2
{
    /// <summary>
    /// A form for professor data manipulation.
    /// </summary>
    public partial class ProfForm : Form
    {

        private MainForm _parent;
        private ProfDao _profDao;

        /// <summary>
        /// Main constructor.
        /// </summary>
        /// <param name="parent">The parent MainForm that will be frozen.</param>
        public ProfForm(MainForm parent)
        {
            InitializeComponent();

            _parent = parent;
            _parent.Enabled = false;
            _profDao = new ProfDao();
        }

        private void ProfForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _parent.Enabled = true;
        }

        private void ProfForm_Load(object sender, EventArgs e)
        {
            LoadAll();
            SetInputStates(false);
            cbNew.Enabled = false;
        }

        /// <summary>
        /// Fills the form with data.
        /// </summary>
        private void LoadAll()
        {
            Utilities.FillDgv(_profDao, dgvProfessors);

            List<Rank> rankList = _profDao.GetRankList();
            cbRank.DataSource = rankList;
            cbRank.DisplayMember = "RankName";
            cbRank.ValueMember = "RankId";
        }

        /// <summary>
        /// Sets the input widgets state.
        /// </summary>
        /// <param name="state">The new state.</param>
        private void SetInputStates(Boolean state)
        {
            tbCnp.Enabled = state;
            tbLname.Enabled = state;
            tbFname.Enabled = state;
            tbTel.Enabled = state;
            tbEmail.Enabled = state;
            cbRank.Enabled = state;
            btSave.Enabled = state;
            btCancel.Enabled = state;

            dgvProfessors.Enabled = !state;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            cbNew.Checked = true;
            SetInputStates(true);
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            if (dgvProfessors.SelectedRows.Count == 1)
            {
                cbNew.Checked = false;
                tbCnp.Text = dgvProfessors.SelectedRows[0].Cells[0].Value.ToString();
                tbLname.Text = dgvProfessors.SelectedRows[0].Cells[1].Value.ToString();
                tbFname.Text = dgvProfessors.SelectedRows[0].Cells[2].Value.ToString();
                tbTel.Text = dgvProfessors.SelectedRows[0].Cells[3].Value.ToString();
                tbEmail.Text = dgvProfessors.SelectedRows[0].Cells[4].Value.ToString();
                cbRank.Text = dgvProfessors.SelectedRows[0].Cells[5].Value.ToString();

                SetInputStates(true);
                tbCnp.Enabled = false; // No modification of CNP is allowed.
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            cbNew.Checked = false;
            SetInputStates(false);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (!CorrectInput())
            {
                MessageBox.Show("The input is not correct!");
            }
            else
            {
                if (cbNew.Checked)
                {
                    _profDao.InsertProf(tbCnp.Text, tbLname.Text, tbFname.Text, tbTel.Text, tbEmail.Text,
                        Convert.ToInt32(cbRank.SelectedValue));
                }
                else
                {
                    _profDao.UpdateProf(tbCnp.Text, tbLname.Text, tbFname.Text, tbTel.Text, tbEmail.Text,
                        Convert.ToInt32(cbRank.SelectedValue));
                }
                if (_profDao.Err != "OK")
                    MessageBox.Show(_profDao.Err);

                Utilities.FillDgv(_profDao, dgvProfessors);
                cbNew.Checked = false;
                SetInputStates(false);
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (dgvProfessors.SelectedRows.Count == 1)
            {
                String cnp = dgvProfessors.SelectedRows[0].Cells[0].Value.ToString();
                DialogResult dlg = MessageBox.Show("Delete item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    _profDao.DeleteProf(cnp);
                }
                Utilities.FillDgv(_profDao, dgvProfessors);
            }
        }

        /// <summary>
        /// Tells if the input textboxes have correct values.
        /// </summary>
        /// <returns>True if the input is correct and false otherwise.</returns>
        private bool CorrectInput()
        {
            return tbCnp.Text != "" && tbEmail.Text != "" && tbFname.Text != "" && 
                tbLname.Text != "" && tbTel.Text != "";
        }
    }
}
